package com.efast.dmom.aps.service.impl;

import java.time.LocalDate;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.dmom.aps.bean.Bom;
import com.efast.dmom.aps.bean.BomItem;
import com.efast.dmom.aps.bean.Part;
import com.efast.dmom.aps.bean.Plan;
import com.efast.dmom.aps.bean.PlanOrder;
import com.efast.dmom.aps.bean.ResultBean;
import com.efast.dmom.aps.constant.ServiceConstants;
import com.efast.dmom.aps.datahandlers.PlanDataHandler;
import com.efast.dmom.aps.datahandlers.PlanOrderDataHandler;
import com.efast.dmom.aps.entity.PlanOrderEntity;
import com.efast.dmom.aps.service.PartService;
import com.efast.dmom.aps.service.PlanService;

/**
 * 
 * @author WJ modify
 * @Data 2020/2/18
 *
 */
@Service
public class PlanServiceImpl implements  PlanService{
	@Autowired
	private PartService partService;
	
	@Autowired
	private PlanOrderDataHandler planOrderDataHandler;
	@Autowired
	private PlanDataHandler planDataHandler;
	
	@Override
	public ResultBean decompositionByPlan(Plan plan) {
		plan = planDataHandler.getById(plan.getInnerEntity().getId());
		
		//根据Plan生成1级PlanOrder(计划工单) 主计划工单
		ResultBean resultBean = new ResultBean(true,' ');
		String planOrderNo = plan.getInnerEntity().getPlanNo();
		String planOrderPartNo = plan.getInnerEntity().getPartNo();
		String planOrderPartNoRevision = plan.getInnerEntity().getPartRevision();
		String planOrderPartDesc = plan.getInnerEntity().getPartDesc();
		double planOrderQty    = plan.getInnerEntity().getQty();
		int planOrderPriority = plan.getInnerEntity().getPriority();
		String partDesc = plan.getInnerEntity().getPartDesc();
		LocalDate offlineDate = plan.getInnerEntity().getOfflineDate();
		long plantId = plan.getInnerEntity().getPlantId();
		PlanOrder planOrder = new PlanOrder();
		PlanOrderEntity entity = new PlanOrderEntity();
		entity.setOrderNumber(planOrderNo);
		entity.setPartNo(planOrderPartNo);
		entity.setPartRevision(planOrderPartNoRevision);
		entity.setPartDesc(partDesc);
		entity.setPlantId(plantId);
		entity.setPlanOfflineDate(offlineDate);
		entity.setPartDesc(planOrderPartDesc);
		entity.setQuantity((int)planOrderQty);
		entity.setPriority(planOrderPriority);
		entity.setOrderStatus(ServiceConstants.APS_PLAN_ORDER_STATUS_CREATED);
		planOrder.setInnerData(entity);
		
		planOrderDataHandler.save(planOrder);
		
		//根据BOM分解子工单
		createPlanOrder(planOrderNo,planOrderPartNo,planOrderPartNoRevision,planOrderQty,offlineDate,planOrderPriority+1,plantId);
		plan.getInnerEntity().setStatus("DECOMPOSITION");
		planDataHandler.update(plan);
		resultBean.setMessage("计划订单分解成功");
		return resultBean;
	}
	
	public void createPlanOrder(String orderNo,String partNo,String partRevision,double qty,LocalDate offlineDate,int priority,long plantId) {
 		Part part = partService.getByPartNoAndRevision(partNo, partRevision);
		Bom bom = part.getBom();
		if(bom==null ||bom.getInnerData()==null) {
			return;
		}
		List<BomItem> items = bom.getItems();
		if(items==null || items.size()<1) {
			return;
		}
		for(BomItem bomItem:items) {
			
			String partNo2 = bomItem.getInnerData().getPartNo();
			String partRevision2 = bomItem.getInnerData().getPartRevision();
			double qtyBom = bomItem.getInnerData().getQty();
			
			int planOrderItemQty = (int) (qty*qtyBom);
			
			Part part2 = partService.getByPartNoAndRevision(partNo2, partRevision2);
			if(part2.getInnerData()==null) {
				continue ;
			}
			String type = part2.getInnerData().getType();
			//raw materials(原材料)(RM)    semi-finished products(半成品)(SFP)    finished products(成品)(FP)
			//半成品或者成品的，必须有BOM才分解，没有BOM默认为外购件
			int i=1;
			if(("SFP".equals(type) || "FP".equals(type))&& part2.getBom().getInnerData()!=null) {
				//String orderNoItem = orderNo+String.format("%02d", i++);
				String[] orderNo2 = getOrderNo(orderNo,i);
				String orderNoItem = orderNo2[0];
				i = Integer.valueOf(orderNo2[1]);
				
				PlanOrder planOrder = new PlanOrder();
				PlanOrderEntity entity = new PlanOrderEntity();
				
				entity.setOrderNumber(orderNoItem);
				entity.setPartNo(partNo2);
				entity.setPartRevision(partRevision2);
				entity.setPartDesc(part2.getInnerData().getDescription());
				entity.setQuantity(planOrderItemQty);
				entity.setPriority(priority);
				entity.setPlantId(plantId);
				entity.setOrderStatus(ServiceConstants.APS_PLAN_ORDER_STATUS_CREATED);
				entity.setPlanOfflineDate(offlineDate);
				planOrder.setInnerData(entity);
				
				planOrderDataHandler.save(planOrder);
				createPlanOrder(orderNoItem,partNo2,partRevision2, planOrderItemQty,offlineDate,priority+1,plantId);
			}
		}
	}
	
	private String[] getOrderNo(String orderNo,int i) {
		if(i<1) {
			i=1;
		}
		int length = Integer.toString(i).length();
		if(length <2) {
			length=2;
		}
		String orderNoItem = orderNo+String.format("%0"+length+"d", i++);
		
		PlanOrder byOrderNo = planOrderDataHandler.getByOrderNo(orderNoItem);
		if(byOrderNo!=null && byOrderNo.getInnerData()!=null) {
			String[] orderNo2 = getOrderNo(orderNo,i);
			orderNoItem = orderNo2[0];
			i = Integer.valueOf(orderNo2[1]);
		}
		
		return new String[] {orderNoItem,Integer.toString(i)};
		
	}
	
	@Override
	public ResultBean save(Plan plan) {
		ResultBean resultBean = new ResultBean(true,' ');
		Plan byPlanNo = planDataHandler.getByPlanNo(plan.getInnerEntity().getPlanNo());
		if(byPlanNo!=null) {
			planDataHandler.save(plan);
			resultBean.setSuccess(true);
			resultBean.setMessage("计划订单创建成功!");
		}else {
			resultBean.setSuccess(false);
			resultBean.setMessage("计划订单创建失败，计划订单号重复!");
		}
		return resultBean;
	}

	@Override
	public ResultBean delete(Plan plan) {
		ResultBean resultBean = new ResultBean(true,' ');
		Plan byPlanNo = planDataHandler.getByPlanNo(plan.getInnerEntity().getPlanNo());
		if(byPlanNo!=null) {
			String status = byPlanNo.getInnerEntity().getStatus();
			if("CREATION".equals(status)) {
				planDataHandler.delete(plan);
				resultBean.setSuccess(true);
				resultBean.setMessage("计划订单删除成功!");
			}else {
				resultBean.setSuccess(false);
				resultBean.setMessage("计划订单已分解!");
			}
		}else {
			resultBean.setSuccess(false);
			resultBean.setMessage("计划订单不存在!");
		}
		return resultBean;	
		
	}

	@Override
	public ResultBean update(Plan plan) {
		ResultBean resultBean = new ResultBean(true,' ');
		Plan byPlanNo = planDataHandler.getByPlanNo(plan.getInnerEntity().getPlanNo());
		if(byPlanNo!=null) {
			String status = byPlanNo.getInnerEntity().getStatus();
			if("CREATION".equals(status)) {
	//			plan.getInnerEntity().setStatus("DECOMPOSITION");
				planDataHandler.update(plan);
				resultBean.setSuccess(true);
				resultBean.setMessage("计划订单更新成功!");
			}else {
				resultBean.setSuccess(false);
				resultBean.setMessage("计划订单已分解!");
			}
		}else {
			resultBean.setSuccess(false);
			resultBean.setMessage("计划订单不存在!");
		}
		return resultBean;	
	}

	@Override
	public Plan getById(long id) {
		return planDataHandler.getById(id);
	}

	@Override
	public Plan getByName(String name) {
		// TODO Auto-generated method stub
		return planDataHandler.getByName(name);
	}

	@Override
	public List<Plan> getByStatus(String status) {
		// TODO Auto-generated method stub
		return planDataHandler.getByStatus(status);
	}

	@Override
	public Plan getByPlanNo(String planNo) {
		return planDataHandler.getByPlanNo(planNo);
	}



}


